//
//  CommentsViewController.m
//  OrderSeat
//
//  Created by 王威 on 14-1-7.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "CommentsViewController.h"
#import "CommCell.h"

@implementation CommentsViewController

- (id)initWithId:(NSString*)pid andType:(CommType)type
{
    self = [super initWithNibClass:[CommentsViewController class]];
    if (self) {
        _pid = pid;
        _type = type;
        _dataSource = [[CommentsDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    [_dataSource fetchCommentsIsLoadMore:NO];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"用户点评"];
    
    self.commentsTable.delegate = _dataSource;
    self.commentsTable.dataSource = _dataSource;
}

- (void)appendTableWith:(NSMutableArray *)data
{
    for (int i = 0; i < [data count]; i++) {
        [_dataSource.commentsList addObject:[data objectAtIndex:i]];
    }
    
    [self.commentsTable reloadData];
    /*
    NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];
    
    for (int ind = 0; ind < [data count]; ind++) {
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:[_dataSource.commentsList indexOfObject:[data objectAtIndex:ind]] inSection:0];
        [insertIndexPaths addObject:newPath];
    }
    
    [_commentsTable insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];*/
    self.moreLoading = NO;
}

@end

//=========================================================================================
#pragma ===================数据源类实现======================================================
//=========================================================================================

@implementation CommentsDataSource

#define FETCH_NEW_TAG  1001
#define FETCH_MORE_TAG 1002
#define CommCellId  @"CommCell"

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if (self) {
        self.viewController = viewController;
        self.commentsList = [[NSMutableArray alloc] initWithCapacity:10];
        self.commentCell = [_viewController createCellWithNib:CommCellId];
    }
    return self;
}

- (void)fetchCommentsIsLoadMore:(BOOL)isLoadMore
{
    NSString *api = S_EMPTY;
    switch (_viewController.type) {
        case CouponCommType: api = API_COUPON_COMMS; break;
        case GourmetCommType: api = API_GOURMET_COMMS; break;
        case ActivityCommType: api = API_ACTIVITY_COMMS; break;
        case PhotoCommType: api = API_PHOTO_COMMS; break;
        default: break;
    }
    
    if (!isLoadMore) [SVProgressHUD show];
    
    _pageNumber = isLoadMore ? _pageNumber : 1;
    NSString *url = [NSString stringWithFormat:api,_viewController.pid, _pageNumber];
    [self get:url tag:isLoadMore ? FETCH_MORE_TAG : FETCH_NEW_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    
    [SVProgressHUD dismiss];
    NSDictionary *json = [request.responseData JSONValue];
    NSDictionary *data = [json objectForKey:@"data"];
    
    NSMutableArray *pinglun = [data objectForKey:@"pinglun"];
    if (request.tag == FETCH_NEW_TAG) {
        self.commentsList = pinglun;
        [self.viewController.commentsTable reloadData];
    }
    
    if (request.tag == FETCH_MORE_TAG) {
        [_viewController appendTableWith:pinglun];
    }
}

#pragma ===================加载tableview数据 Begin===========================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_commentCell setContent:[self.commentsList objectAtIndex:indexPath.row]];
    return _commentCell.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _commentsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommCell *cell = [_viewController createCellWithNib:CommCellId tableview:tableView];
    [cell setContent:[self.commentsList objectAtIndex:indexPath.row]];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma 分页加载
-(void)tableView:(UITableView *)tableView  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == _commentsList.count-1 && _commentsList.count >= 10) {
        if (!self.viewController.moreLoading) {
            self.pageNumber++;
            [self fetchCommentsIsLoadMore:YES];
            self.viewController.moreLoading = YES;
        }
    }
}

@end
